package com.dyh.controller;
//引用user实体类
import com.dyh.po.user;
/*引用userService类*/
import com.dyh.service.userService;
/*引用springframework框架*/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
/*引用servlet包*/
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 用户控制器，用于用户登录与用户管理用例控制
 */
@Controller
public class userController {
    @Autowired
    private userService userservice;

    /**
     * 通过用户名查找用户
     * @param request
     * @param model
     * @param session
     * @return String
     */
    @RequestMapping(value = "/login.action", method = RequestMethod.POST)
    public String findUserByUserName(HttpServletRequest request, Model model, HttpSession session){
        String username = request.getParameter("name");  //获取用户名
        String password = request.getParameter("password");  //获取密码
        user loginUser = userservice.findUserByUserName(username,password);  //登录验证
        Long status = loginUser.getRoleId(); //获取用户角色类型
        session.setAttribute("USER_SESSION",loginUser);  //登录用户信息存储
        //角色判断并进行跳转
        if (status==1){
            return "admin";
        }
        else if (status==2){
            return "xznhjbr";
        }
        else if (status == 3){
            return "xhgbjbr";
        }
        else if (status == 4){
            return "xhgbld";
        }
        //登录失败
        model.addAttribute("user",loginUser);
        return "login";
    }

    /**
     * 查找所有用户
     * @param model
     * @return String
     */
    @RequestMapping(value = "userList")
    public String userList(Model model){
        List<user> users = userservice.findAllUser();
        System.out.println(users);
        model.addAttribute("UserList", users);
        return "/user/userlist";
    }

    /**
     * 跳转至用户修改页
     * @param request
     * @return ModelAndView
     */
    @RequestMapping(value = "userEdit")
    public ModelAndView toUserEdit(HttpServletRequest request){
        try {
            request.setCharacterEncoding("UTF-8");
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        ModelAndView mv = new ModelAndView();
        String uid = request.getParameter("u_id");
        user user = userservice.findUserById(uid);
        mv.addObject("User",user);
        mv.setViewName("user/userEdit");
        return mv;
    }

    /**
     * 修改用户
     * @param request
     * @return String
     */
    @RequestMapping(value = "userEditAction")
    public String editUser(HttpServletRequest request){
        try {
            request.setCharacterEncoding("UTF-8");
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        String username = request.getParameter("id");
        String userpwd = request.getParameter("password");
        String fullname = request.getParameter("fullname");
        String email = request.getParameter("email");
        String telephone = request.getParameter("telephone");
        String role = request.getParameter("roleid");
        Long roleid = Long.valueOf(role);
        int row = userservice.editUser(username,userpwd,fullname,email,telephone,roleid);
        if(row>0){
            return "redirect:/userList";
        }else{
            return "user/userEdit";
        }
    }

    /**
     * 跳转至用户添加页
     * @return String
     */
    @RequestMapping(value = "userAdd")
    public String toUserAdd(){
        return "user/userAdd";
    }
    @RequestMapping(value = "userAddAction")
    public String addUser(HttpServletRequest request,Model model){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String fullname = request.getParameter("fullname");
        String email = request.getParameter("email");
        String telephone = request.getParameter("telephone");
        String role = request.getParameter("roleid");
        Long roleid = Long.valueOf(role);
        int row=userservice.addIntoUser(username,password,fullname,email,telephone);
        System.out.println("row="+row);
        if(row>0){
            row=userservice.addIntoUserRole(username,roleid);
        }if(row>0){
            List<user> user = userservice.findAllUser();
            System.out.println(user);
            model.addAttribute("UserList", user);
            return "/user/userlist";
        }else{
            return "user/userAdd";
        }
    }

    /**
     * 删除用户动作
     * @param request
     * @return String
     */
    @RequestMapping(value = "userDeleteAction")
    public String deleteUser(HttpServletRequest request){
        String username = request.getParameter("u_id");
        int row = userservice.deleteUser(username);
        return "redirect:/userList";
    }
}
